const Sampler = require("./Sampler");
const PreProcessor = require("./PreProcessor");
const PostProcessor = require("./PostProcessor");
const Assertions = require("./Assertions");
const GroovyScript = require("../GroovyScript");


/**
 * 自定义
 * 
 */
class Custom {

    /**
     * 
     * @param {XMLBuilder} parent 
     * @param {{inputs: {redisKey: string, redisVariableNames: string[], jmxVariableNames: string[], isReusable: boolean}[], largeValue: {testPlanName: string}}} config 
     * @returns {XMLBuilder}
     */
    createInitialization(parent, config = { inputs: [], largeValue: null }) {
        const jmxInitialization = new Sampler().createDebugSampler(parent, { testname: "初始化", comments: "" });
        if (config.inputs.length > 0) {
            config.inputs.forEach(input => {
                const { redisKey, redisVariableNames, jmxVariableNames, isReusable } = input;
                new PreProcessor().createJsr223PreProcessor(jmxInitialization,
                    { testname: `输入变量(${redisKey})`, comments: "", script: GroovyScript.getInputScript(redisKey, redisVariableNames, jmxVariableNames, isReusable) });
            });
        }
        if (config.largeValue) {
            const { testPlanName } = config.largeValue;
            new PreProcessor().createJsr223PreProcessor(jmxInitialization,
                { testname: "大值变量", comments: "", script: GroovyScript.getLargeValuesScript(testPlanName) });
        }
        return jmxInitialization;
    }

    /**
     * 
     * @param {XMLBuilder} parent 
     * @param {{outputs: {redisKey: string, redisVariableNames: string[], jmxVariableNames: string[]}[]}} config 
     * @returns {XMLBuilder}
     */
    createFinalization(parent, config = { outputs: [] }) {
        const jmxFinalization = new Sampler().createDebugSampler(parent, { testname: "完成", comments: "" });
        if (config.outputs.length > 0) {
            config.outputs.forEach(output => {
                const { redisKey, redisVariableNames, jmxVariableNames } = output;
                new Assertions().createJsr223Assertion(jmxFinalization,
                    { testname: `完成断言(${redisKey})`, script: GroovyScript.getFinalizationAssertScript(jmxVariableNames) }); 
                new PostProcessor().createJsr223PostProcessor(jmxFinalization,
                    { testname: `输出变量(${redisKey})`, comments: "", script: GroovyScript.getOutputScript(redisKey, redisVariableNames, jmxVariableNames) });
            });
        }
        return jmxFinalization;
    }

}

module.exports = Custom;

